Skip to content

Merge tag 'REL_16_9' into Cloudberry#1760

Open
chenjinbao1989 wants to merge 5733 commits into
apache:mainfrom
chenjinbao1989:cbdb-pg16-merge
Open

Merge tag 'REL_16_9' into Cloudberry#1760
chenjinbao1989 wants to merge 5733 commits into
apache:mainfrom
chenjinbao1989:cbdb-pg16-merge

Conversation

@chenjinbao1989
Copy link
Copy Markdown
Contributor

Merge upstream PostgreSQL REL_16_9 into Cloudberry, upgrading the
kernel from PG 14.4 to PG 16.9. This spans two major versions (PG 15
and PG 16), bringing in 5730 commits.

Key features and changes, sorted by importance:

  1. MERGE command

    • SQL-standard MERGE for INSERT/UPDATE/DELETE in a single statement.
    • ruleutils decompilation, PlaceHolderVar support in actions
    • Fixes for partitioned tables, cross-partition triggers, RLS,
    • EvalPlanQual, self-modified tuples, pg_stat_statements
  2. Logical replication enhancements

    • Built-in support for prepared transactions (2PC)
    • Enable two-phase via replication protocol
    • Streaming changes after speculative aborts
    • New pg_create_subscription predefined role
    • Parallel apply workers (PG 16)
    • Row/column filtering, schema-level publication (PG 15)
    • Critical fix for data loss in logical replication
    • Slot invalidation, snapshot, and memory leak fixes
  3. SQL/JSON

    • IS JSON predicate
    • Standard JSON constructor functions (json_array, json_object,
      json_scalar, json_serialize)
    • Auto-generated query jumbling via gen_node_support.pl
    • Fix json_array() subquery double transformation
  4. WAL and storage subsystem

    • LZ4 compression for full-page writes (FPW)
    • Custom WAL resource managers
    • Skip WAL recycling/preallocation during archive recovery
    • XLOG_FPI_FOR_HINT honors full_page_writes setting
    • Direct I/O support on macOS
    • Buffer manager infrastructure refactor for faster relation extension
    • New smgrzeroextend(), FileZero(), FileFallocate()
    • Remove HeapBitmapScan skip_fetch optimization (correctness issue)
    • WAL/Recovery separation: xlogrecovery.c, xlogprefetcher.c (PG 15)
    • Backup sink architecture (PG 15)
  5. Query optimizer / executor
    Performance:

    • Hash table acceleration for NOT IN(values)
    • Datum sorts for single-column sorts
    • Result Cache renamed to Memoize with ongoing fixes
    • Parallel Hash Full Join
    • Reduced planning cost for deeply-nested views
    • Fewer pallocs when building partition bounds
    • Outer join optimization enhancement (PG 16)
    • Symmetric hash join optimization (PG 16)
      Critical fixes:
    • Planner failure to identify multiple hashable ScalarArrayOpExprs
    • Consistent whole-row Var construction in parsing and planning
    • WindowAgg evaluation crash
    • WITH RECURSIVE UNION assert failure
    • ORDER BY / DISTINCT aggregates with FILTER
    • setrefs.c missing expression processing on prune steps
    • Parallel Hash Join extreme skew detection
  6. pgstat monitoring enhancements
    pgstat shared memory rewrite (PG 15):

    • Stats collector process removed; stats now in shared memory
    • Monolithic pgstat.c split into 14 files under utils/activity/
      pg_stat_io (PG 16 new view):
    • Detailed I/O statistics: shared buffer hits, I/O times, writeback
      pg_stat_statements:
    • JIT counters, temp file block I/O timing
    • Utility query string normalization, MERGE support
    • 32-bit integer overflow fix
      Other:
    • pg_stat_wal time accumulation as instr_time
    • Macro-generated pg_stat_get*() functions for tables and databases
    • Enhanced pg_stat_reset_single_table_counters
    • SP-GiST index scans counted in pg_stat
    • New test helpers: pg_stat_force_next_flush(), pg_stat_have_stats()
  7. libpq and client protocol

    • New PQsendFlushRequest
    • Pipeline mode state machine fix
    • SASL code refactored to generic interface
    • Escape function fixes for invalid encoding data (security)
    • PQescapeLiteral()/PQescapeIdentifier() length handling fix
    • New pg_encoding_set_invalid()
    • Build-time check that libpq doesn't call exit()/abort()
  8. Security and privilege model

    • CREATEROLE privilege restrictions (major security improvement)
    • New GUCs: createrole_self_grant, reserved_connections
    • New predefined roles: pg_maintain, pg_create_subscription
    • Non-superuser predefined roles for vacuum/analyze
    • Revoke PUBLIC CREATE from public schema (now owned by pg_database_owner)
    • Security invoker views (SECURITY INVOKER)
    • session_authorization and role interaction fixes
    • scram_SaltedPassword() integer overflow fix
  9. pg_dump / pg_upgrade / pg_basebackup toolchain
    pg_dump:

    • Generic compression API, zstd support, LZ4 frame-only format
      pg_basebackup:
    • Extended compression options, server-side compression with -Fp
    • Client-side LZ4 decompression, parallel zstd compression
      pg_upgrade:
    • Fix unintentional 'NULL' string literal
    • Fix XMLSERIALIZE(NO INDENT) cross-version upgrade
    • pg_dumpall handling of dangling OIDs in pg_auth_members
  10. Partitioned table improvements

    • Self-referencing FKs in partitioned tables
    • Detach partition with top-level FK fix
    • Reset relhassubclass on ATTACH
    • Trigger rename consistency, preserve firing state on clone
    • Disallow partitionwise join/grouping on collation mismatch
  11. ICU and collation

    • Build ICU support by default
    • initdb uses uloc_getDefault()
    • CREATE DATABASE LOCALE applies to all collation providers
  12. Node support function auto-generation (FIXME)

    • gen_node_support.pl auto-generates copyfuncs/equalfuncs/outfuncs/readfuncs
    • Query jumbling code auto-generation
    • NodeTag ABI stability check
  13. Global renames and structural changes

    • RelFileNode -> RelFileLocator (pervasive rename across hundreds of files)
    • GUC system split: guc.c -> guc.c + guc_tables.c + guc_funcs.c
    • varatt.h separated from postgres.h
    • Test framework: PostgresNode.pm -> PostgreSQL::Test::Cluster
  14. SQL types and functions

    • Numeric scale allows negative or greater than precision
    • New ANY_VALUE aggregate function
    • unnest(multirange), range_agg with multirange inputs
    • pg_size_pretty/pg_size_bytes support petabytes
    • ALTER TABLE SET ACCESS METHOD
    • SYSTEM_USER function
    • numeric_mul() overflow fix
  15. psql improvements

    • PSQL_WATCH_PAGER for \watch command
    • New \drg command for role grants
    • \copy from sends data in larger chunks
    • pg_waldump --rmgr multiple specification
  16. Critical data corruption and crash fixes

    • Fix data corruption when relation truncation fails
    • Fail instead of corrupting page header on non-LP_NORMAL TID update
    • Fix unsafe BufferDescriptors access
    • GB18030 SIGSEGV from out-of-bounds read
    • Snowball stemmer null pointer dereference after OOM
    • Rare standby assertion failure on primary restart
    • catcache invalidation during list entry construction
  17. Vacuum / Autovacuum

    • Parallel VACUUM (PG 16, vacuumparallel.c new file)
    • ON COMMIT DELETE ROWS avoids ERROR after relhassubclass=f
    • Use WaitLatch() instead of pg_usleep() at end-of-vacuum truncation
    • Prevent numeric overflow in parallel numeric aggregates
    • ANALYZE preserves relhasindex for partitioned tables

Co-authored-by: liushengsong lss602726449@gmail.com
Co-authored-by: reshke reshke@double.cloud
Co-authored-by: Hao Wu gfphoenix78@gmail.com
Co-authored-by: Jianghua.yjh yjhjstz@gmail.com
Co-authored-by: Dianjin Wang wangdianjin@gmail.com

alvherre and others added 30 commits November 7, 2024 14:06
The previous wording is easy to read incorrectly; this change makes it
simpler, less ambiguous, and less prominent.

Backpatch to all live branches.

Reviewed-by: Amit Langote <amitlangote09@gmail.com>
Discussion: https://postgr.es/m/202411051201.zody6mld7vkw@alvherre.pgsql
Backpatch the part of edee0c621de that applies to a90bdd7a44d, which
was also backpatched.  That way, the message is consistent in all
branches.
If the collation of any grouping column doesn’t match the collation of
the corresponding partition key, partitionwise grouping can yield
incorrect results. For example, rows that would be grouped under the
grouping collation may end up in different partitions under the
partitioning collation. In such cases, full partitionwise grouping
would produce results that differ from those without partitionwise
grouping, so disallowed that.

Partial partitionwise aggregation is still allowed, as the Finalize
step reconciles partition-level aggregates with grouping requirements
across all partitions, ensuring that the final output remains
consistent.

This commit also fixes group_by_has_partkey() by ensuring the
RelabelType node is stripped from grouping expressions when matching
them to partition key expressions to avoid false mismatches.

Bug: #18568
Reported-by: Webbo Han <1105066510@qq.com>
Author: Webbo Han <1105066510@qq.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Aleksander Alekseev <aleksander@timescale.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Discussion: https://postgr.es/m/18568-2a9afb6b9f7e6ed3@postgresql.org
Discussion: https://postgr.es/m/tencent_9D9103CDA420C07768349CC1DFF88465F90A@qq.com
Discussion: https://postgr.es/m/CAHewXNno_HKiQ6PqyLYfuqDtwp7KKHZiH1J7Pqyz0nr+PS2Dwg@mail.gmail.com
Backpatch-through: 12
If the collation of any join key column doesn’t match the collation of
the corresponding partition key, partitionwise joins can yield incorrect
results. For example, rows that would match under the join key collation
might be located in different partitions due to the partitioning
collation. In such cases, a partitionwise join would yield different
results from a non-partitionwise join, so disallow it in such cases.

Reported-by: Tender Wang <tndrwang@gmail.com>
Author: Jian He <jian.universality@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Junwang Zhao <zhjwpku@gmail.com>
Discussion: https://postgr.es/m/CAHewXNno_HKiQ6PqyLYfuqDtwp7KKHZiH1J7Pqyz0nr+PS2Dwg@mail.gmail.com
Backpatch-through: 12
Commit ac04aa84a put the shutoff for this into the planner, which is
not ideal because it doesn't prevent us from re-using a previously
made parallel plan.  Revert the planner change and instead put the
shutoff into InitializeParallelDSM, modeling it on the existing code
there for recovering from failure to allocate a DSM segment.

However, that code path is mostly untested, and testing a bit harder
showed there's at least one bug: ExecHashJoinReInitializeDSM is not
prepared for us to have skipped doing parallel DSM setup.  I also
thought the Assert in ReinitializeParallelWorkers is pretty
ill-advised, and replaced it with a silent Min() operation.

The existing test case added by ac04aa84a serves fine to test this
version of the fix, so no change needed there.

Patch by me, but thanks to Noah Misch for the core idea that we
could shut off worker creation when !INTERRUPTS_CAN_BE_PROCESSED.
Back-patch to v12, as ac04aa84a was.

Discussion: https://postgr.es/m/CAC-SaSzHUKT=vZJ8MPxYdC_URPfax+yoA1hKTcF4ROz_Q6z0_Q@mail.gmail.com
This commit changes libpq so that errors reported by the backend during
the protocol negotiation for SSL and GSS are discarded by the client, as
these may include bytes that could be consumed by the client and write
arbitrary bytes to a client's terminal.

A failure with the SSL negotiation now leads to an error immediately
reported, without a retry on any other methods allowed, like a fallback
to a plaintext connection.

A failure with GSS discards the error message received, and we allow a
fallback as it may be possible that the error is caused by a connection
attempt with a pre-11 server, GSS encryption having been introduced in
v12.  This was a problem only with v17 and newer versions; older
versions discard the error message already in this case, assuming a
failure caused by a lack of support for GSS encryption.

Author: Jacob Champion
Reviewed-by: Peter Eisentraut, Heikki Linnakangas, Michael Paquier
Security: CVE-2024-10977
Backpatch-through: 12
Source-Git-URL: https://git.postgresql.org/git/pgtranslation/messages.git
Source-Git-Hash: 2bf252d27e0167b62b663baaab5e9b4c773ba9de
Many process environment variables (e.g. PATH), bypass the containment
expected of a trusted PL.  Hence, trusted PLs must not offer features
that achieve setenv().  Otherwise, an attacker having USAGE privilege on
the language often can achieve arbitrary code execution, even if the
attacker lacks a database server operating system user.

To fix PL/Perl, replace trusted PL/Perl %ENV with a tied hash that just
replaces each modification attempt with a warning.  Sites that reach
these warnings should evaluate the application-specific implications of
proceeding without the environment modification:

  Can the application reasonably proceed without the modification?

    If no, switch to plperlu or another approach.

    If yes, the application should change the code to stop attempting
    environment modifications.  If that's too difficult, add "untie
    %main::ENV" in any code executed before the warning.  For example,
    one might add it to the start of the affected function or even to
    the plperl.on_plperl_init setting.

In passing, link to Perl's guidance about the Perl features behind the
security posture of PL/Perl.

Back-patch to v12 (all supported versions).

Andrew Dunstan and Noah Misch

Security: CVE-2024-10979
If a CTE, subquery, sublink, security invoker view, or coercion
projection references a table with row-level security policies, we
neglected to mark the plan as potentially dependent on which role
is executing it.  This could lead to later executions in the same
session returning or hiding rows that should have been hidden or
returned instead.

Reported-by: Wolfgang Walther
Reviewed-by: Noah Misch
Security: CVE-2024-10976
Backpatch-through: 12
The SQL spec mandates that SET SESSION AUTHORIZATION implies
SET ROLE NONE.  We tried to implement that within the lowest-level
functions that manipulate these settings, but that was a bad idea.
In particular, guc.c assumes that it doesn't matter in what order
it applies GUC variable updates, but that was not the case for these
two variables.  This problem, compounded by some hackish attempts to
work around it, led to some security-grade issues:

* Rolling back a transaction that had done SET SESSION AUTHORIZATION
would revert to SET ROLE NONE, even if that had not been the previous
state, so that the effective user ID might now be different from what
it had been.

* The same for SET SESSION AUTHORIZATION in a function SET clause.

* If a parallel worker inspected current_setting('role'), it saw
"none" even when it should see something else.

Also, although the parallel worker startup code intended to cope
with the current role's pg_authid row having disappeared, its
implementation of that was incomplete so it would still fail.

Fix by fully separating the miscinit.c functions that assign
session_authorization from those that assign role.  To implement the
spec's requirement, teach set_config_option itself to perform "SET
ROLE NONE" when it sets session_authorization.  (This is undoubtedly
ugly, but the alternatives seem worse.  In particular, there's no way
to do it within assign_session_authorization without incompatible
changes in the API for GUC assign hooks.)  Also, improve
ParallelWorkerMain to directly set all the relevant user-ID variables
instead of relying on some of them to get set indirectly.  That
allows us to survive not finding the pg_authid row during worker
startup.

In v16 and earlier, this includes back-patching 9987a7bf3 which
fixed a violation of GUC coding rules: SetSessionAuthorization
is not an appropriate place to be throwing errors from.

Security: CVE-2024-10978
meson makes the backslashes in text2macro.pl's --strip argument
into forward slashes, effectively disabling comment stripping.
That hasn't caused us issues before, but it breaks the test case
for b7e3a52a8.  We don't really need the pattern to be adjustable,
so just hard-wire it into the script instead.

Context: mesonbuild/meson#1564
Security: CVE-2024-10979
Ooops, missed that v16 has another text2macro call in the MSVC scripts.

Security: CVE-2024-10979
v16 commit 8fe3e69 used REGRESS_OPTS in
a way needing this.  That broke "vcregress plcheck".  Back-patch
v16..v12; newer versions don't have this build system.
TestUpgradeXversion knows how to make the main regression database's
references to pg_regress.so be version-independent.  But it doesn't
do that for plperl's database, so that the C function added by
commit b7e3a52a8 is causing cross-version upgrade test failures.
Path of least resistance is to just drop the function at the end
of the new test.

In <= v14, also take the opportunity to clean up the generated
test files.

Security: CVE-2024-10979
…cks.

Commit 5a2fed911 had an unexpected side-effect: the parallel worker
launched for the new test case would fail if it couldn't use a
superuser-reserved connection slot.  The reason that test failed
while all our pre-existing ones worked is that the connection
privilege tests in InitPostgres had been based on the superuserness
of the leader's AuthenticatedUserId, but after the rearrangements
of 5a2fed911 we were testing the superuserness of CurrentUserId,
which the new test case deliberately made to be a non-superuser.

This all seems very accidental and probably not the behavior we really
want, but a security patch is no time to be redesigning things.
Pending some discussion about desirable semantics, hack it so that
InitPostgres continues to pay attention to the superuserness of
AuthenticatedUserId when starting a parallel worker.

Nathan Bossart and Tom Lane, per buildfarm member sawshark.

Security: CVE-2024-10978
The current code calls array_eq() and does not provide FmgrInfo.  This commit
provides initialization of FmgrInfo and uses C collation as the safe option
for text comparison because we don't know anything about the semantics of
opclass options.

Backpatch to 13, where opclass options were introduced.

Reported-by: Nicolas Maus
Discussion: https://postgr.es/m/18692-72ea398df3ec6712%40postgresql.org
Backpatch-through: 13
Maintain the pg_stat_user_indexes.idx_scan pgstat counter during
contrib/Bloom index scans.

Oversight in commit 9ee014f, which added the Bloom index contrib
module.

Author: Masahiro Ikeda <ikedamsh@oss.nttdata.com>
Reviewed-By: Peter Geoghegan <pg@bowt.ie>
Discussion: https://postgr.es/m/c48839d881388ee401a01807c686004d@oss.nttdata.com
Backpatch: 13- (all supported branches).
This fixes a set of race conditions with cumulative statistics where a
shared stats entry could be dropped while it should still be valid in
the event when it is reused: an entry may refer to a different object
but requires the same hash key.  This can happen with various stats
kinds, like:
- Replication slots that compute internally an index number, for
different slot names.
- Stats kinds that use an OID in the object key, where a wraparound
causes the same key to be used if an OID is used for the same object.
- As of PostgreSQL 18, custom pgstats kinds could also be an issue,
depending on their implementation.

This issue is fixed by introducing a counter called "generation" in the
shared entries via PgStatShared_HashEntry, initialized at 0 when an
entry is created and incremented when the same entry is reused, to avoid
concurrent issues on drop because of other backends still holding a
reference to it.  This "generation" is copied to the local copy that a
backend holds when looking at an object, then cross-checked with the
shared entry to make sure that the entry is not dropped even if its
"refcount" justifies that if it has been reused.

This problem could show up when a backend shuts down and needs to
discard any entries it still holds, causing statistics to be removed
when they should not, or even an assertion failure.  Another report
involved a failure in a standby after an OID wraparound, where the
startup process would FATAL on a "can only drop stats once", stopping
recovery abruptly.  The buildfarm has been sporadically complaining
about the problem, as well, but the window is hard to reach with the
in-core tests.

Note that the issue can be reproduced easily by adding a sleep before
dshash_find() in pgstat_release_entry_ref() to enlarge the problematic
window while repeating test_decoding's isolation test oldest_xmin a
couple of times, for example, as pointed out by Alexander Lakhin.

Reported-by: Alexander Lakhin, Peter Smith
Author: Kyotaro Horiguchi, Michael Paquier
Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/CAA4eK1KxuMVyAryz_Vk5yq3ejgKYcL6F45Hj9ZnMNBS-g+PuZg@mail.gmail.com
Discussion: https://postgr.es/m/17947-b9554521ad963c9c@postgresql.org
Backpatch-through: 15
Previously, in unlucky cases, it was possible for pg_rewind to remove
certain WAL segments from the rewound demoted primary.  In particular
this happens if those files have been marked for archival (i.e., their
.ready files were created) but not yet archived; the newly promoted node
no longer has such files because of them having been recycled, but they
are likely critical for recovery in the demoted node.  If pg_rewind
removes them, recovery is not possible anymore.

Fix this by maintaining a hash table of files in this situation in the
scan that looks for a checkpoint, which the decide_file_actions phase
can consult so that it knows to preserve them.

Backpatch to 14.  The problem also exists in 13, but that branch was not
blessed with commit eb00f1d, so this patch is difficult to apply
there.  Users of older releases will just have to continue to be extra
careful when rewinding.

Co-authored-by: Полина Бунгина (Polina Bungina) <bungina@gmail.com>
Co-authored-by: Alexander Kukushkin <cyberdemn@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Discussion: https://postgr.es/m/CAAtGL4AhzmBRsEsaDdz7065T+k+BscNadfTqP1NcPmsqwA5HBw@mail.gmail.com
In commit 08c0d6a which introduced "rainbow" arcs in regex NFAs,
I didn't think terribly hard about what to do when creating the color
complement of a rainbow arc.  Clearly, the complement cannot match any
characters, and I took the easy way out by just not building any arcs
at all in the complement arc set.  That mostly works, but Nikolay
Shaplov found a case where it doesn't: if we decide to delete that
sub-NFA later because it's inside a "{0}" quantifier, delsub()
suffered an assertion failure.  That's because delsub() relies on
the target sub-NFA being fully connected.  That was always true
before, and the best fix seems to be to restore that property.
Hence, invent a new arc type CANTMATCH that can be generated in
place of an empty color complement, and drop it again later when we
start NFA optimization.  (At that point we don't need to do delsub()
any more, and besides there are other cases where NFA optimization can
lead to disconnected subgraphs.)

It appears that this bug has no consequences in a non-assert-enabled
build: there will be some transiently leaked NFA states/arcs, but
they'll get cleaned up eventually.  Still, we don't like assertion
failures, so back-patch to v14 where rainbow arcs were introduced.

Per bug #18708 from Nikolay Shaplov.

Discussion: https://postgr.es/m/18708-f94f2599c9d2c005@postgresql.org
…kwards.

Previously LogicalIncreaseRestartDecodingForSlot() accidentally
accepted any LSN as the candidate_lsn and candidate_valid after the
restart_lsn of the replication slot was updated, so it potentially
caused the restart_lsn to move backwards.

A scenario where this could happen in logical replication is: after a
logical replication restart, based on previous candidate_lsn and
candidate_valid values in memory, the restart_lsn advances upon
receiving a subscriber acknowledgment. Then, logical decoding restarts
from an older point, setting candidate_lsn and candidate_valid based
on an old RUNNING_XACTS record. Subsequent subscriber acknowledgments
then update the restart_lsn to an LSN older than the current value.

In the reported case, after WAL files were removed by a checkpoint,
the retreated restart_lsn prevented logical replication from
restarting due to missing WAL segments.

This change essentially modifies the 'if' condition to 'else if'
condition within the function. The previous code had an asymmetry in
this regard compared to LogicalIncreaseXminForSlot(), which does
almost the same thing for different fields.

The WAL removal issue was reported by Hubert Depesz Lubaczewski.

Backpatch to all supported versions, since the bug exists since 9.4
where logical decoding was introduced.

Reviewed-by: Tomas Vondra, Ashutosh Bapat, Amit Kapila
Discussion: https://postgr.es/m/Yz2hivgyjS1RfMKs%40depesz.com
Discussion: https://postgr.es/m/85fff40e-148b-4e86-b921-b4b846289132%40vondra.me
Backpatch-through: 13
After commit 5a2fed911a85ed6d8a015a6bafe3a0d9a69334ae, the catalog state
resulting from these commands ceased to affect sessions.  Restore the
longstanding behavior, which is like beginning the session with a SET
ROLE command.  If cherry-picking the CVE-2024-10978 fixes, default to
including this, too.  (This fixes an unintended side effect of fixing
CVE-2024-10978.)  Back-patch to v12, like that commit.  The release team
decided to include v12, despite the original intent to halt v12 commits
earlier this week.

Tom Lane and Noah Misch.  Reported by Etienne LAFARGE.

Discussion: https://postgr.es/m/CADOZwSb0UsEr4_UTFXC5k7=fyyK8uKXekucd+-uuGjJsGBfxgw@mail.gmail.com
Commits aac2c9b4f et al. added a bool field to struct ResultRelInfo.
That's no problem in the master branch, but in released branches
care must be taken when modifying publicly-visible structs to avoid
an ABI break for extensions.  Frequently we solve that by adding the
new field at the end of the struct, and that's what was done here.
But ResultRelInfo has stricter constraints than just about any other
node type in Postgres.  Some executor APIs require extensions to index
into arrays of ResultRelInfo, which means that any change whatever in
sizeof(ResultRelInfo) causes a fatal ABI break.

Fortunately, this is easy to fix, because the new field can be
squeezed into available padding space instead --- indeed, that's where
it was put in master, so this fix also removes a cross-branch coding
variation.

Per report from Pavan Deolasee.  Patch v14-v17 only; earlier versions
did not gain the extra field, nor is there any problem in master.

Discussion: https://postgr.es/m/CABOikdNmVBC1LL6pY26dyxAS2f+gLZvTsNt=2XbcyG7WxXVBBQ@mail.gmail.com
In the dim past we figured it was okay to ignore collations
when combining UNION set-operation nodes into a single N-way
UNION operation.  I believe that was fine at the time, but
it stopped being fine when we added nondeterministic collations:
the semantics of distinct-ness are affected by those.  v17 made
it even less fine by allowing per-child sorting operations to
be merged via MergeAppend, although I think we accidentally
avoided any live bug from that.

Add a check that collations match before deciding that two
UNION nodes are equivalent.  I also failed to resist the
temptation to comment plan_union_children() a little better.

Back-patch to all supported branches (v13 now), since they
all have nondeterministic collations.

Discussion: https://postgr.es/m/3605568.1731970579@sss.pgh.pa.us
In 17~, age(xid) and mxid_age(xid) were listed as deprecated.  Based on
the discussion that led to 48b5aa3143, this is not intentional as this
could break many existing monitoring queries.  Note that vacuumdb also
uses both of them.

In 16, both functions were listed under "Control Data Functions", which
is incorrect, so let's move them to the list of functions related to
transaction IDs and snapshots.

Author: Bertrand Drouvot
Discussion: https://postgr.es/m/Zzr2zZFyeFKXWe8a@ip-10-97-1-34.eu-west-3.compute.internal
Discussion: https://postgr.es/m/20231114013224.4z6oxa6p6va33rxr@awork3.anarazel.de
Backpatch-through: 16
david-rowley and others added 20 commits April 22, 2025 14:57
The previous text was a little clumsy.  Here we improve that.

Author: David Rowley <dgrowleyml@gmail.com>
Reported-by: Noboru Saito <noborusai@gmail.com>
Reviewed-by: David G. Johnston <david.g.johnston@gmail.com>
Discussion: https://postgr.es/m/CAAM3qnJtv5YbjpwDfVOYN2gZ9zGSLFM1UGJgptSXmwfifOZJFQ@mail.gmail.com
Backpatch-through: 13
This assertion, based on pending_since (timestamp used to prevent stats
reports to be too frequent or should a partial flush happen), is reached
when it is found that no data can be flushed but a previous call of
pgstat_report_stat() determined that some stats data has been found as
in need of a flush.  So pending_since is set when some stats data is
pending (in non-force mode) or if report attempts are too frequent, and
reset to 0 once all stats have been flushed.

Since 5cbbe70a9cc6, WAL senders have begun to report their stats on a
periodic basis for IO stats in v16~ and backend stats on HEAD, creating
some friction with the concurrent pgstat_report_stat() calls that can
happen in the context of a WAL sender (shutdown callback doing a final
report or backend-related code paths).  This problem is the cause of
spurious failures in the TAP tests.

In theory, this assertion can be also reached in v15, even if that's
very unlikely.  For example, a process, say a background worker, could
do periodic and direct stats flushes with concurrent calls of
pgstat_report_stat() that could cause conflicting values of
pending_since.  This can be done with WAL or SLRU stats flushes using
pgstat_flush_wal() or pgstat_slru_flush().  HEAD makes this situation
easier to happen with custom cumulative stats.

This commit removes the assertion altogether, per discussion, as it is
more useful to keep the state of things as they are for the WAL sender.
The assertion could use a special state based on for example
am_walsender, but I doubt that this would be meaningful in the long run
based on the other arguments raised while discussing this issue.

Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reported-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/1489124.1744685908@sss.pgh.pa.us
Discussion: https://postgr.es/m/dwrkeszz6czvtkxzr5mqlciy652zau5qqnm3cp5f3p2po74ppk@omg4g3cc6dgq
Backpatch-through: 15
Commit 3f28b2fcac tried to ensure that the replication origin shouldn't be
advanced in case of an ERROR in the apply worker, so that it can request
the same data again after restart. However, it is possible that an ERROR
was caught and handled by a (say PL/pgSQL) function, and  the apply worker
continues to apply further changes, in which case, we shouldn't reset the
replication origin.

Ensure to reset the origin only when the apply worker exits after an
ERROR.

Commit 3f28b2fcac added new function geterrlevel, which we removed in HEAD
as part of this commit, but kept it in backbranches to avoid breaking any
applications. A separate case can be made to have such a function even for
HEAD.

Reported-by: Shawn McCoy <shawn.the.mccoy@gmail.com>
Author: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: vignesh C <vignesh21@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Backpatch-through: 16, where it was introduced
Discussion: https://postgr.es/m/CALsgZNCGARa2mcYNVTSj9uoPcJo-tPuWUGECReKpNgTpo31_Pw@mail.gmail.com
One place in hash_create() used DynaHashAlloc() as a convenient
shorthand for MemoryContextAlloc().  That was fine when it was
written, but it stopped being fine when 9c911ec changed
DynaHashAlloc() to use MCXT_ALLOC_NO_OOM (mea culpa).  Change
the code to call plain MemoryContextAlloc() as intended.

I think that this bug may be unreachable in practice, since we now
always create AllocSets with some space already allocated, so that
an OOM failure here for a non-shared hash table should be impossible
(with a hash table name of reasonable length anyway).  And there
aren't enough shared hash tables to make a crash for one of those
probable.  Nonetheless it's clearly not operating as designed, so
back-patch to v16 where 9c911ec came in.

Reported-by: Maksim Korotkov <m.korotkov@postgrespro.ru>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/219bdccd460510efaccf90b57e5e5ef2@postgrespro.ru
Backpatch-through: 16
During logical decoding, we advance catalog_xmin of logical too early in
fast_forward mode, resulting in required catalog data being removed by
vacuum. This mode is normally used to advance the slot without processing
the changes, but we still can't let the slot's xmin to advance to an
incorrect value.

Commit f49a80c fixed a similar issue where the logical slot's
catalog_xmin was getting advanced prematurely during non-fast-forward
mode. During xl_running_xacts processing, instead of directly advancing
the slot's xmin to the oldest running xid in the record, it allowed the
xmin to be held back for snapshots that can be used for
not-yet-replayed transactions, as those might consider older txns as
running too. However, it missed the fact that the same problem can happen
during fast_forward mode decoding, as we won't build a base snapshot in
that mode, and the future call to get_changes from the same slot can miss
seeing the required catalog changes leading to incorrect reslts.

This commit allows building the base snapshot even in fast_forward mode to
prevent the early advancement of xmin.

Reported-by: Amit Kapila <amit.kapila16@gmail.com>
Author: Zhijie Hou <houzj.fnst@fujitsu.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: shveta malik <shveta.malik@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Backpatch-through: 13
Discussion: https://postgr.es/m/CAA4eK1LqWncUOqKijiafe+Ypt1gQAQRjctKLMY953J79xDBgAg@mail.gmail.com
Discussion: https://postgr.es/m/OS0PR01MB57163087F86621D44D9A72BF94BB2@OS0PR01MB5716.jpnprd01.prod.outlook.com
DST law changes in Chile: there is a new time zone America/Coyhaique
for Chile's Aysén Region, to account for it changing to UTC-03
year-round and thus diverging from America/Santiago.

Historical corrections for Iran.

Backpatch-through: 13
Add a documentation warning to ts_headline() pointing out that, when
working with untrusted input documents, the output is not guaranteed
to be safe for direct inclusion in web pages. This is because, while
it does remove some XML tags from the input, it doesn't remove all
HTML markup, and so the result may be unsafe (e.g., it might permit
XSS attacks).

To guard against that, all HTML markup should be removed from the
input, making it plain text, or the output should be passed through an
HTML sanitizer.

In addition, document precisely what the default text search parser
recognises as valid XML tags, since that's what determines which XML
tags ts_headline() will remove.

Reported-by: Richard Neill <richard.neill@telos.digital>
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Noah Misch <noah@leadboat.com>
Backpatch-through: 13
SQL "SET search_path = 'pg_catalog, pg_temp'" is silently equivalent to
"SET search_path = pg_temp, pg_catalog, "pg_catalog, pg_temp"" instead
of the intended "SET search_path = pg_catalog, pg_temp".  (The intent
was a two-element search path.  With the single quotes, it instead
specifies one element with a comma and a space in the middle of the
element.)  In addition to the SET statement, this affects SET clauses of
CREATE FUNCTION, ALTER ROLE, and ALTER DATABASE.  It does not affect the
set_config() SQL function.

Though the documentation did not show an insecure command, remove single
quotes that could entice a reader to write an insecure command.
Back-patch to v13 (all supported versions).

Reported-by: Sven Klemm <sven@timescale.com>
Author: Sven Klemm <sven@timescale.com>
Backpatch-through: 13
It's --auto-features not --auto_features.

Reported-by: Egor Chindyaskin <kyzevan23@mail.ru>
Discussion: https://postgr.es/m/172465652540.862882.17808523044292761256@wrigleys.postgresql.org
Discussion: https://postgr.es/m/1979661.1746212726@sss.pgh.pa.us
Backpatch-through: 16
For self-referencing foreign keys in partitioned tables, we weren't
handling creation of pg_constraint rows during CREATE TABLE PARTITION AS
as well as ALTER TABLE ATTACH PARTITION.  This is an old bug -- mostly,
we broke this in 614a406 while trying to fix it (so 12.13, 13.9,
14.6 and 15.0 and up all behave incorrectly).  This commit reverts part
of that with additional fixes for full correctness, and installs more
tests to verify the parts we broke, not just the catalog contents but
also the user-visible behavior.

Backpatch to all live branches.  In branches 13 and 14, commit
46a8c27a7226 changed the behavior during DETACH to drop a FK
constraint rather than trying to repair it, because the complete fix of
repairing catalog constraints was problematic due to lack of previous
fixes.  For this reason, the test behavior in those branches is a bit
different.  However, as best as I can tell, the fix works correctly
there.

In release notes we have to recommend that all self-referencing foreign
keys on partitioned tables be recreated if partitions have been created
or attached after the FK was created, keeping in mind that violating
rows might already be present on the referencing side.

Reported-by: Guillaume Lelarge <guillaume@lelarge.info>
Reported-by: Matthew Gabeler-Lee <fastcat@gmail.com>
Reported-by: Luca Vallisa <luca.vallisa@gmail.com>
Discussion: https://postgr.es/m/CAECtzeWHCA+6tTcm2Oh2+g7fURUJpLZb-=pRXgeWJ-Pi+VU=_w@mail.gmail.com
Discussion: https://postgr.es/m/18156-a44bc7096f0683e6@postgresql.org
Discussion: https://postgr.es/m/CAAT=myvsiF-Attja5DcWoUWh21R12R-sfXECY2-3ynt8kaOqjw@mail.gmail.com
Also adjust the phrasing in the comments.

Author: Etsuro Fujita <etsuro.fujita@gmail.com>
Author: Heikki Linnakangas <hlinnaka@iki.fi>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Gurjeet Singh <gurjeet@singh.im>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAPmGK17%3DPHSDZ%2B0G6jcj12buyyE1bQQc3sbp1Wxri7tODT-SDw%40mail.gmail.com
Backpatch-through: 15
Source-Git-URL: https://git.postgresql.org/git/pgtranslation/messages.git
Source-Git-Hash: 73452f0d3ca43035a492ff657802cc9060561413
Start the file with static functions not specific to pe_test_vectors
tests.  This way, new tests can use them without disrupting the file's
layout.  Change report_result() PQExpBuffer arguments to plain strings.
Back-patch to v13 (all supported versions), for the next commit.

Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Backpatch-through: 13
Security: CVE-2025-4207
With GB18030 as source encoding, applications could crash the server via
SQL functions convert() or convert_from().  Applications themselves
could crash after passing unterminated GB18030 input to libpq functions
PQescapeLiteral(), PQescapeIdentifier(), PQescapeStringConn(), or
PQescapeString().  Extension code could crash by passing unterminated
GB18030 input to jsonapi.h functions.  All those functions have been
intended to handle untrusted, unterminated input safely.

A crash required allocating the input such that the last byte of the
allocation was the last byte of a virtual memory page.  Some malloc()
implementations take measures against that, making the SIGSEGV hard to
reach.  Back-patch to v13 (all supported versions).

Author: Noah Misch <noah@leadboat.com>
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Backpatch-through: 13
Security: CVE-2025-4207
Merge upstream PostgreSQL REL_16_9 into Cloudberry, upgrading the
kernel from PG 14.4 to PG 16.9. This spans two major versions (PG 15
and PG 16), bringing in 5730 commits.

Key features and changes, sorted by importance:

1. MERGE command
   - SQL-standard MERGE for INSERT/UPDATE/DELETE in a single statement.
   - ruleutils decompilation, PlaceHolderVar support in actions
   - Fixes for partitioned tables, cross-partition triggers, RLS,
   - EvalPlanQual, self-modified tuples, pg_stat_statements

2. Logical replication enhancements
   - Built-in support for prepared transactions (2PC)
   - Enable two-phase via replication protocol
   - Streaming changes after speculative aborts
   - New pg_create_subscription predefined role
   - Parallel apply workers (PG 16)
   - Row/column filtering, schema-level publication (PG 15)
   - Critical fix for data loss in logical replication
   - Slot invalidation, snapshot, and memory leak fixes

3. SQL/JSON
   - IS JSON predicate
   - Standard JSON constructor functions (json_array, json_object,
     json_scalar, json_serialize)
   - Auto-generated query jumbling via gen_node_support.pl
   - Fix json_array() subquery double transformation

4. WAL and storage subsystem
   - LZ4 compression for full-page writes (FPW)
   - Custom WAL resource managers
   - Skip WAL recycling/preallocation during archive recovery
   - XLOG_FPI_FOR_HINT honors full_page_writes setting
   - Direct I/O support on macOS
   - Buffer manager infrastructure refactor for faster relation extension
   - New smgrzeroextend(), FileZero(), FileFallocate()
   - Remove HeapBitmapScan skip_fetch optimization (correctness issue)
   - WAL/Recovery separation: xlogrecovery.c, xlogprefetcher.c (PG 15)
   - Backup sink architecture (PG 15)

5. Query optimizer / executor
   Performance:
   - Hash table acceleration for NOT IN(values)
   - Datum sorts for single-column sorts
   - Result Cache renamed to Memoize with ongoing fixes
   - Parallel Hash Full Join
   - Reduced planning cost for deeply-nested views
   - Fewer pallocs when building partition bounds
   - Outer join optimization enhancement (PG 16)
   - Symmetric hash join optimization (PG 16)
   Critical fixes:
   - Planner failure to identify multiple hashable ScalarArrayOpExprs
   - Consistent whole-row Var construction in parsing and planning
   - WindowAgg evaluation crash
   - WITH RECURSIVE UNION assert failure
   - ORDER BY / DISTINCT aggregates with FILTER
   - setrefs.c missing expression processing on prune steps
   - Parallel Hash Join extreme skew detection

6. pgstat monitoring enhancements
   pgstat shared memory rewrite (PG 15):
   - Stats collector process removed; stats now in shared memory
   - Monolithic pgstat.c split into 14 files under utils/activity/
   pg_stat_io (PG 16 new view):
   - Detailed I/O statistics: shared buffer hits, I/O times, writeback
   pg_stat_statements:
   - JIT counters, temp file block I/O timing
   - Utility query string normalization, MERGE support
   - 32-bit integer overflow fix
   Other:
   - pg_stat_wal time accumulation as instr_time
   - Macro-generated pg_stat_get*() functions for tables and databases
   - Enhanced pg_stat_reset_single_table_counters
   - SP-GiST index scans counted in pg_stat
   - New test helpers: pg_stat_force_next_flush(), pg_stat_have_stats()

7. libpq and client protocol
   - New PQsendFlushRequest
   - Pipeline mode state machine fix
   - SASL code refactored to generic interface
   - Escape function fixes for invalid encoding data (security)
   - PQescapeLiteral()/PQescapeIdentifier() length handling fix
   - New pg_encoding_set_invalid()
   - Build-time check that libpq doesn't call exit()/abort()

8. Security and privilege model
   - CREATEROLE privilege restrictions (major security improvement)
   - New GUCs: createrole_self_grant, reserved_connections
   - New predefined roles: pg_maintain, pg_create_subscription
   - Non-superuser predefined roles for vacuum/analyze
   - Revoke PUBLIC CREATE from public schema (now owned by pg_database_owner)
   - Security invoker views (SECURITY INVOKER)
   - session_authorization and role interaction fixes
   - scram_SaltedPassword() integer overflow fix

9. pg_dump / pg_upgrade / pg_basebackup toolchain
    pg_dump:
    - Generic compression API, zstd support, LZ4 frame-only format
    pg_basebackup:
    - Extended compression options, server-side compression with -Fp
    - Client-side LZ4 decompression, parallel zstd compression
    pg_upgrade:
    - Fix unintentional 'NULL' string literal
    - Fix XMLSERIALIZE(NO INDENT) cross-version upgrade
    - pg_dumpall handling of dangling OIDs in pg_auth_members

10. Partitioned table improvements
    - Self-referencing FKs in partitioned tables
    - Detach partition with top-level FK fix
    - Reset relhassubclass on ATTACH
    - Trigger rename consistency, preserve firing state on clone
    - Disallow partitionwise join/grouping on collation mismatch

11. ICU and collation
    - Build ICU support by default
    - initdb uses uloc_getDefault()
    - CREATE DATABASE LOCALE applies to all collation providers

12. Node support function auto-generation (FIXME)
    - gen_node_support.pl auto-generates copyfuncs/equalfuncs/outfuncs/readfuncs
    - Query jumbling code auto-generation
    - NodeTag ABI stability check

13. Global renames and structural changes
    - RelFileNode -> RelFileLocator (pervasive rename across hundreds of files)
    - GUC system split: guc.c -> guc.c + guc_tables.c + guc_funcs.c
    - varatt.h separated from postgres.h
    - Test framework: PostgresNode.pm -> PostgreSQL::Test::Cluster

14. SQL types and functions
    - Numeric scale allows negative or greater than precision
    - New ANY_VALUE aggregate function
    - unnest(multirange), range_agg with multirange inputs
    - pg_size_pretty/pg_size_bytes support petabytes
    - ALTER TABLE SET ACCESS METHOD
    - SYSTEM_USER function
    - numeric_mul() overflow fix

15. psql improvements
    - PSQL_WATCH_PAGER for \watch command
    - New \drg command for role grants
    - \copy from sends data in larger chunks
    - pg_waldump --rmgr multiple specification

16. Critical data corruption and crash fixes
    - Fix data corruption when relation truncation fails
    - Fail instead of corrupting page header on non-LP_NORMAL TID update
    - Fix unsafe BufferDescriptors access
    - GB18030 SIGSEGV from out-of-bounds read
    - Snowball stemmer null pointer dereference after OOM
    - Rare standby assertion failure on primary restart
    - catcache invalidation during list entry construction

17. Vacuum / Autovacuum
    - Parallel VACUUM (PG 16, vacuumparallel.c new file)
    - ON COMMIT DELETE ROWS avoids ERROR after relhassubclass=f
    - Use WaitLatch() instead of pg_usleep() at end-of-vacuum truncation
    - Prevent numeric overflow in parallel numeric aggregates
    - ANALYZE preserves relhasindex for partitioned tables

Co-authored-by: liushengsong <lss602726449@gmail.com>
Co-authored-by: reshke <reshke@double.cloud>
Co-authored-by: Hao Wu <gfphoenix78@gmail.com>
Co-authored-by: Jianghua.yjh <yjhjstz@gmail.com>
Co-authored-by: Dianjin Wang <wangdianjin@gmail.com>
@chenjinbao1989 chenjinbao1989 changed the title Merge tag 'REL_16_9' into Cloudberry [test] Merge tag 'REL_16_9' into Cloudberry May 21, 2026
@chenjinbao1989 chenjinbao1989 changed the title [test] Merge tag 'REL_16_9' into Cloudberry Merge tag 'REL_16_9' into Cloudberry May 22, 2026
@yjhjstz
Copy link
Copy Markdown
Member

yjhjstz commented May 23, 2026

@tuhaihe
Copy link
Copy Markdown
Member

tuhaihe commented May 25, 2026

Great !

related extensions tested ?

I think this should be a follow-up work to adopt PG16 support for them.

Comment thread .github/workflows/apache-rat-audit.yml Outdated
on:
push:
branches: [main, REL_2_STABLE]
branches: [main, cbdb-postgres-merge]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can restore the target branch from cbdb-postgres-merge to REL_2_STABLE, since cbdb-postgres-merge is only for the development branch test, right?

Suggested change
branches: [main, cbdb-postgres-merge]
branches: [main, REL_2_STABLE]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be removed. We don't use it anymore.

Comment thread GNUmakefile.in Outdated
Comment on lines +75 to +81
# $(MAKE) -C contrib/hstore $@
# $(MAKE) -C contrib/pgcrypto $@
# $(MAKE) -C contrib/btree_gin $@
# $(MAKE) -C contrib/pg_trgm $@
# $(MAKE) -C contrib/tablefunc $@
# $(MAKE) -C contrib/passwordcheck $@
# $(MAKE) -C contrib/pg_buffercache $@
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, is it okay to enable these contribs by now? Or do we plan to make follow-up fixes after this PR is merged?

Comment thread .cirrus.star Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the file .cirrus.star, .cirrus.tasks.yml & .cirrus.yml, which are only used for PG tests.

@my-ship-it
Copy link
Copy Markdown
Contributor

Great work!! Thanks @chenjinbao1989 and @lss602726449 !!!

Copy link
Copy Markdown
Contributor

@avamingli avamingli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks~ LGTM.

- Remove Cirrus CI configuration files (.cirrus.star, .cirrus.tasks.yml,
  .cirrus.yml) as the project no longer uses Cirrus CI
- Remove pg16-merge-validation GitHub Actions workflow, no longer needed
- Update apache-rat-audit branch references from cbdb-postgres-merge
  to REL_2_STABLE
- Re-enable contrib modules in GNUmakefile.in that were commented out
  during the PG16 merge: auto_explain, formatter_fixedwidth,
  fuzzystrmatch, dblink, indexscan, hstore, pgcrypto, btree_gin,
  pg_trgm, tablefunc, passwordcheck, pg_buffercache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.